const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
function getDistancs(cx, cy, pointer_x, pointer_y) {
    var ox = pointer_x - cx;
    var oy = pointer_y - cy;
    return Math.sqrt(
        ox * ox + oy * oy
    );
}

/*
 *参数1和2为图片圆心坐标
 *参数3和4为手点击的坐标
  *返回值为手点击的坐标到圆心的角度
 */
function countDeg (cx, cy, pointer_x, pointer_y) {
    var ox = pointer_x - cx;
    var oy = pointer_y - cy;
    var to = Math.abs(ox / oy);
    var angle = Math.atan(to) / (2 * Math.PI) * 360;//鼠标相对于旋转中心的角度
    // console.log("ox.oy:", ox, oy)
    if (ox < 0 && oy < 0)//相对在左上角，第四象限，js中坐标系是从左上角开始的，这里的象限是正常坐标系
    {
        angle = -angle;
    } else if (ox <= 0 && oy >= 0)//左下角,3象限
    {
        angle = -(180 - angle)
    } else if (ox > 0 && oy < 0)//右上角，1象限
    {
        angle = angle;
    } else if (ox > 0 && oy > 0)//右下角，2象限
    {
        angle = 180 - angle;
    }

    return angle;

}
/**
 * 时间秒数格式化
 * @param s 时间戳（单位：秒）
 * @returns {*} 格式化后的时分秒
 */
function secToTimefunction(s) {
    var t;
    if(s > -1&&typeof s != 'undefined'&&s != 'undefined'&&s != undefined){

        var min = Math.floor(s/60) % 60;
        var sec = s % 60;


        if(min < 10){t = "0";}
        t = min + ":";
        if(sec < 10){t += "0";}
        t += sec.toFixed(0);
    }
    return t;
}
function isNotEmptyString(string){
    if (string !=''&&
        typeof string !='undefined'&&
        string!= undefined&&
        string!= '0'
        ) {
        return true;
    }else{
        return false
    }
}
//处理随机专辑数据
function formatNetworkMusicData(res){
    var formatData ={};
    var workInfoData=res.data.data;
    formatData=this.formatNetworkSingleData(workInfoData);
    if (this.isNotEmptyString(workInfoData.ctitle)) {
        formatData.mp3Title = workInfoData.ctitle;
    }else if (this.isNotEmptyString(workInfoData.title)) {
        formatData.mp3Title = workInfoData.title;
    }else if(this.isNotEmptyString(workInfoData.workDesc)){
        formatData.mp3Title = workInfoData.workDesc;
    }else if (this.isNotEmptyString(workInfoData.catalogue.ctitle)){
        formatData.mp3Title = workInfoData.catalogue.ctitle;
    }else{
        formatData.mp3Title = workInfoData.catalogue.title;
    }

    if (this.isNotEmptyString(workInfoData.catalogue.cover500)) {
        formatData.mp3Img = workInfoData.catalogue.cover500;
    }else if (this.isNotEmptyString(workInfoData.catalogue.cover200)) {
        formatData.mp3Img = workInfoData.catalogue.cover200;
    }

    return formatData;
}
//处理专辑数据
function formatNetworkProMusicData(resData){
    var formatData ={};
    // console.log(workData);
    var workInfoData = resData.workInfo;
    if (!this.isNotEmptyString(workInfoData)){
        console.log('error------------本条收藏数据无信息')
        console.log(resData)
        return;
    }
    formatData=this.formatNetworkSingleData(workInfoData);
    if ( this.isNotEmptyString(resData.ctitle)){
        formatData.mp3Title = resData.ctitle;
    }else if ( this.isNotEmptyString(resData.title)){
        formatData.mp3Title = resData.title;
    }else if(this.isNotEmptyString(workInfoData.workDesc)){
        formatData.mp3Title = workInfoData.workDesc;
    }else if (this.isNotEmptyString(workInfoData.catalogue.ctitle)){
        formatData.mp3Title = workInfoData.catalogue.ctitle;
    }else{
        formatData.mp3Title = workInfoData.catalogue.title;
    }

    if (this.isNotEmptyString(resData.card_id)) {
        formatData.cardId = resData.card_id;
        formatData.isShowCard = 0;
    }else {
        formatData.isShowCard = 1;
    }

    formatData.isPlaying = 0;
    formatData.txtStyle = '';

    return formatData;
}
//处理单个音频数据
function formatNetworkSingleData (workInfoData){
    var formatData={}
    if (this.isNotEmptyString(workInfoData.catalogue.cover500)) {
        formatData.mp3Img = workInfoData.catalogue.cover500;
    }else if (this.isNotEmptyString(workInfoData.catalogue.cover200)) {
        formatData.mp3Img = workInfoData.catalogue.cover200;
    }

    formatData.workId = workInfoData.workId;
    formatData.isLike = workInfoData.isFavorite;
    formatData.itemcode = workInfoData.itemcode;


    var itemcodeData=workInfoData.catalogue.itemcode;
    var labelidData=workInfoData.catalogue.labelid;
    var mp3List = [];
    var trackList =workInfoData.tracks;
    for (var j =0;j<trackList.length;j++){
        var mp3SongData={};
        mp3SongData.l_code=trackList[j].l_code;
        mp3SongData.mp3Title=trackList[j].title;
        mp3SongData.itemcode=itemcodeData;
        mp3SongData.labelid=labelidData;
        mp3SongData.mp3Image= formatData.mp3Img;
        if (this.isNotEmptyString(trackList[j].ctitle)){
            mp3SongData.mp3Title = trackList[j].ctitle;
        }
        mp3List.push(mp3SongData);
    }
    formatData.mp3List=mp3List;
    return formatData;
}
//判断手机是否大于X 屏幕进行适配 N正常 X iphoneX以及以上
function systemInfoToPhoneHeightIsX(resource){
    var model = resource.model;
    console.log('手机型号 getSystemInfo'+model)
    if (model.indexOf('iPhone') >= 0 && (model.indexOf('X') >= 0||model.indexOf('11') >= 0||model.indexOf('12') >= 0)) {
        return 'X'
    } else {
        return 'N'
    }
}
module.exports = {
  formatTime: formatTime,
    secToTimefunction:secToTimefunction,
    countDeg:countDeg,
    getDistancs:getDistancs,
    isNotEmptyString:isNotEmptyString,
    formatNetworkMusicData:formatNetworkMusicData,
    formatNetworkProMusicData:formatNetworkProMusicData,
    formatNetworkSingleData:formatNetworkSingleData,
    systemInfoToPhoneHeightIsX:systemInfoToPhoneHeightIsX,
}
